<template>
  <div>
    <h1>商品详情</h1>
  </div>

  <div>
    <el-descriptions title="商品详情">
      <el-descriptions-item label="prodId">{{ prodId }}</el-descriptions-item>
      <el-descriptions-item label="prodSpu">{{
        prod.prodSpu
      }}</el-descriptions-item>
      <el-descriptions-item label="prodName">{{
        prod.prodName
      }}</el-descriptions-item>
      <el-descriptions-item label="prodImage">{{
        prod.prodImage
      }}</el-descriptions-item>
      <el-descriptions-item label="createTime">{{
        prod.createTime
      }}</el-descriptions-item>
      <el-descriptions-item label="prodType">{{
        prod.prodType
      }}</el-descriptions-item>
    </el-descriptions>
  </div>
  <el-button type="primary" @click="toSave()">添加</el-button>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="prodId" label="Date" width="180" />
      <el-table-column prop="prodName" label="Name" width="180" />
      <el-table-column prop="prodSku" label="SKu" width="180" />

      <el-table-column prop="prodStock" label="库存" width="180" />
      <el-table-column prop="prodPrice" label="商品价钱" width="180" />
      <el-table-column prop="costPrice" label="花费价钱" width="180" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column label="操作">
        <template #default="scope"> </template>
      </el-table-column>
    </el-table>
  </div>

  <el-dialog v-model="dialogFormVisible" title="Shipping address" width="500">
    <el-form :model="saveForm">
      <el-form-item label="商品名称" :label-width="formLabelWidth">
        <el-input v-model="saveForm.prodName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="商品Sku" :label-width="formLabelWidth">
        <el-input v-model="saveForm.prodSku" autocomplete="off" />
      </el-form-item>
      <el-form-item label="商品价钱" :label-width="formLabelWidth">
        <el-input v-model="saveForm.prodPrice" autocomplete="off" />
      </el-form-item>
      <el-form-item label="商品库存" :label-width="formLabelWidth">
        <el-input v-model="saveForm.prodStock" autocomplete="off" />
      </el-form-item>
      <el-form-item label="购买数量" :label-width="formLabelWidth">
        <el-input v-model="saveForm.buyNum" autocomplete="off" />
      </el-form-item>
      <el-form-item label="花费价钱" :label-width="formLabelWidth">
        <el-input v-model="saveForm.costPrice" autocomplete="off" />
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:10010/team/index/uploadImage"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="headers"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="SaveProd()"> 添加 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useRoute } from "vue-router";
const route = useRoute();
import { ref } from "vue";
import { getProdInfo, prodInfoList, saveProdInfo } from "@/api/api";
import { ElMessage } from "element-plus";
import { onMounted } from "vue";
const tableData = ref([]);
const prod = ref({
  prodId: null,
  prodSpu: "",
  prodKind: null,
  prodType: null,
  prodName: "",
  brandId: null,
  createBy: "",
  createTime: "",
  updateBy: "",
  updateTime: "",
  unitId: null,
  tagId: null,
  appr: "",
  prodImage: "",
  imageList: null,
});

const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const saveForm = ref({
  buyNum: null,
  costPrice: null,
  createBy: "",
  createTime: "",
  infoId: null,
  prodId: null,
  prodName: "",
  prodPrice: null,
  prodSku: "",
  prodStock: null,
  updateBy: "",
  updateTime: "",
});
const getInfo = () => {
  getProdInfo(prodId).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("商品展示成功");

      prod.value = res.data.data;
      searchForm.value.prodId = res.data.data.prodId;
      saveForm.value.prodId = res.data.data.prodId;
      // saveForm.value.prodSku = res.data.data.prodSpu;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

// searchForm.value.prodId = prodId;
const prodList = () => {
  prodInfoList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("列表展示成功");

      tableData.value = res.data.data.records;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const toSave = () => {
  dialogFormVisible.value = true;
};
const prodId = route.params.prodId;
console.log("111" + prodId);
const searchForm = ref({
  pageNum: 1,
  pageSize: 10,
  prodId: prodId,
});
onMounted(() => {
  getInfo();
  prodList();

  // searchForm.value.prodId = Number(prodId);
});
const SaveProd = () => {
  saveProdInfo(saveForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      prodList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  // imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  imageUrl.value = response.data;
};
const headers = ref({
  token: sessionStorage.getItem("token"),
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
